<?php

// Script from: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html
// saves the weather underground xml data as a string
function getXML($lat, $lon) {
    // header is only needed if we are echoing
    //header('Content-type: application/xml');

    // Website url to open
    $url = 'http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query='.$lat.','.$lon;

    // Get that website's content
    $handle = fopen($url, 'r');

    // get local xml
    $xml = '';
    
    // If there is something, read and return
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            //echo $buffer;
            $xml .= $buffer;
        }
        fclose($handle);
    }
    
    return $xml;
}

// returns the linked CSV file as a string
function getCSV($url, $headerFlag) {
    include 'common.php';

    $body = array();
    $headers = array();
    $headerIndices = array();
    $handle = fopen($url, 'r');
    
    if($handle) {
        // get the headers
        if($headerFlag && !feof($handle)) {
            do { // skip any non-empty lines
                $line = fgetcsv($handle);
            } while (empty($line[0]));
            
            for($i = 0; $i < count($line); $i++) {
                $name = $line[$i];
                $headers[$i] = $name;           // headers[index] = "Name";
                $headerIndices[strtolower($name)] = $i;    // headers["name"] = index
            }
        }
        
        // get the rest of the csv
        $row = 0;
        while(!feof($handle)) {
            $line = fgetcsv($handle);   // an array
            $body[$row] = $line;
            $row++;
        }
        fclose($handle);
    }
    
    if($headerFlag) {
        return array($HEADER=>$headers, $HEADER_INDEX=>$headerIndices, $BODY=>$body);
    } else {
        return $body;
    }
}

// returns the linked CSV file as a string
function getCSVfromString($inputcsv, $headerFlag) {
    include 'common.php';

    $body = array();
    $headers = array();
    $headerIndices = array();
    $lines = explode('%n', $inputcsv);
    
    // get the headers
    if($headerFlag) {
        $line = explode(',', $lines[0]);
    
        for($i = 0; $i < count($line); $i++) {
            $name = $line[$i];
            $headers[$i] = $name;           // headers[index] = "Name";
            $headerIndices[strtolower($name)] = $i;    // headers["name"] = index
        }
    }
    
    // get the rest of the csv
    for($i = 1; $i < count($lines)-1; $i++) {
        $temp = explode(',', $lines[$i]);
        if(!empty($temp)) {
            $body[$i-1] = $temp;
        }
    }
    
    if($headerFlag) {
        return array($HEADER=>$headers, $HEADER_INDEX=>$headerIndices, $BODY=>$body);
    } else {
        return $body;
    }
}

// gets the corresponding row of the weather data based on what the desired date is
function getWeatherRowFromDate($csv, $month, $day, $year) {
    $rowNum = 0; // default in case parsing doesn't reveal anything
    
    foreach ($csv as $row) {
        // parse the date
        $date = date_parse($row[0]);
        if( $month == $date[month] && $day == $date[day] && $year == $date[year]) {
            return $rowNum;
        }
        
        $rowNum++;
    }
    
    return $rowNum;
}

// parses the user's parameters from the url
function getParameters($paramString) {
    $parameters = explode(",", $paramString);
    return $parameters;
}

function printParameters($p) {
    $output = "";
    
    for($i = 0; $i < count($p); $i++) {
        $output .= $p[$i];
        
        if($i < count($p) - 1) {
            $output .= ",";
        }
    }
    return $output;
}
?>
